#version 450 core

out vec4 ocolor;

layout(location = 0)  uniform vec3      iResolution;           // viewport resolution (in pixels)
layout(location = 1)  uniform float     iTime;                 // shader playback time (in seconds)
layout(location = 2)  uniform float     iTimeDelta;            // render time (in seconds)
layout(location = 3)  uniform int       iFrame;                // shader playback frame
layout(location = 4)  uniform float     iChannelTime[4];       // channel playback time (in seconds)
layout(location = 8)  uniform vec3      iChannelResolution[4]; // channel resolution (in pixels)
layout(location = 12) uniform vec4      iMouse;                // mouse pixel coords. xy: current (if MLB down), zw: click
layout(location = 13) uniform vec4      iDate;                 // (year, month, day, time in seconds)
layout(location = 14) uniform float     iSampleRate;           // sound sample rate (i.e., 44100)

layout(binding = 0) uniform sampler2D	iChannel0;
layout(binding = 1) uniform sampler2D	iChannel1;
layout(binding = 2) uniform sampler2D	iChannel2;
layout(binding = 3) uniform sampler2D	iChannel3;

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{	
    // Normalized pixel coordinates (from 0 to 1)
    //vec2 uv = fragCoord/iResolution.xy;
    vec2 uv = fragCoord/vec2(400,400);

    // Time varying pixel color
    vec3 col = 0.5 + 0.5*cos(iTime+uv.xyx+vec3(0,2,4));

    // Output to screen
    fragColor = vec4(col,1.0);
}

vec3 hash3( vec2 p )
{
    vec3 q = vec3( dot(p,vec2(127.1,311.7)), 
				   dot(p,vec2(269.5,183.3)), 
				   dot(p,vec2(419.2,371.9)) );
	return fract(sin(q)*43758.5453);
}

float voronoise( in vec2 p, float u, float v )
{
	float k = 1.0+63.0*pow(1.0-v,6.0);

    vec2 i = floor(p);
    vec2 f = fract(p);
    
	vec2 a = vec2(0.0,0.0);
    for( int y=-2; y<=2; y++ )
    for( int x=-2; x<=2; x++ )
    {
        vec2  g = vec2( x, y );
		vec3  o = hash3( i + g )*vec3(u,u,1.0);
		vec2  d = g - f + o.xy;
		float w = pow( 1.0-smoothstep(0.0,1.414,length(d)), k );
		a += vec2(o.z*w,w);
    }
	
    return a.x/a.y;
}

void mainImage___( out vec4 fragColor, in vec2 fragCoord )
{
	vec2 uv = fragCoord / iResolution.xx;

    vec2 p = 0.5 - 0.5*cos( iTime*vec2(1.0,0.5) );
    
	if( iMouse.w>0.001 ) p = vec2(0.0,1.0) + vec2(1.0,-1.0)*iMouse.xy/iResolution.xy;
	
	p = p*p*(3.0-2.0*p);
	p = p*p*(3.0-2.0*p);
	p = p*p*(3.0-2.0*p);
	
	float f = voronoise( 24.0*uv, p.x, p.y );
	
	fragColor = vec4( f, f, f, 1.0 );
}

void main()
{
	
	mainImage(ocolor,gl_FragCoord.xy);
}